/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "Timer.h"

Timer* g_pTimer = NULL;

bool coreInitTimer()
{
	if (g_pTimer) 
		DELETE_POINTER( g_pTimer );
	
	g_pTimer = new Timer();
	return true;
}

Timer* coreGetTimer()
{
	return g_pTimer;
}

Timer::Timer(void)
:	bPaused(false)
{
	QueryPerformanceFrequency(&lFreq);
	reset();
}

Timer::~Timer(void)
{
}

void Timer::reset()
{
	QueryPerformanceCounter(&lStartTime);
	lLastTime = lStartTime;
	bPaused = false;
}

void Timer::pause()
{
	if (bPaused) return;

	QueryPerformanceCounter(&lPauseTime);
	bPaused = true;
}

void Timer::unpause()
{
	if (!bPaused) return;

	QueryPerformanceCounter(&lNow);
	lStartTime.QuadPart += lNow.QuadPart - lPauseTime.QuadPart;
	bPaused = false;
}

double Timer::getTime()
{
	if (bPaused)
		return double(lPauseTime.QuadPart - lStartTime.QuadPart) / lFreq.QuadPart;
	else {
		QueryPerformanceCounter(&lNow);
		return double(lNow.QuadPart - lStartTime.QuadPart) / lFreq.QuadPart;
	}
}

double Timer::getTimeDiff()
{
	if (bPaused)
		return 0.0;
	else {
		QueryPerformanceCounter(&lNow);
		lDiff.QuadPart = lNow.QuadPart - lLastTime.QuadPart;

		lLastTime.QuadPart = lNow.QuadPart;

		dDiff = double(lDiff.QuadPart) / lFreq.QuadPart; 

		// Assure that the time step is not too large
		if (dDiff > 0.2) dDiff = 0.2;

		return dDiff;
	}
}

double Timer::getLastTimeDiff()
{
	return dDiff;
}

const bool Timer::isPaused() const
{
	return bPaused;
}

int Timer::getFPS()
{
	static int nCounter = 0;
	static int nFPS = 0;
	static double fTimeNow;

	fTimeNow = static_cast<double>( GetTickCount() );
	static double fOldTime = fTimeNow;

	++nCounter;

	if( fOldTime+1000.0 < fTimeNow ) {
		fOldTime = fTimeNow;
		nFPS = nCounter;
		nCounter = 0;
	}

	return nFPS;
}

void Timer::Destroy()
{
	DELETE_POINTER( g_pTimer );
}
